package cn.he4966.ws.demos.web;

import cn.he4966.ws.wss.WebSocketService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.lang.reflect.Array;

@Controller
public class ApiController {


    @RequestMapping("/send_ws")
    @ResponseBody
    public String send_ws(HttpServletRequest request, @RequestParam(name = "group", defaultValue = "he4966.cn") String group, @RequestParam(name = "msg", defaultValue = "hello") String msg) throws IOException {
        //获取http客户端ip
        String ip = request.getRemoteAddr();
        System.out.print("ip:" + ip);
        System.out.println(group + ":" + msg);

        //给ws群组发送消息
        Integer count = WebSocketService.groupMsg(group, msg);
        return "{\"code\":1,\"count\":" + count + "}";
    }
}
